import React,{Component} from 'react'
import {RegisterContainer,
    RegisterTitle,
    RegisterMidContainer,
    RegisterBtn,
    RegisterTips
}from './RegisterStyle'
import cookie from '../../../utils/cookie'
import {Toast} from 'antd-mobile'
import HomeCommon from 'pages/home/HomeCommon'

class Register extends Component{
    //注册
    reg=()=>{
         let obj = [];
        const username = this.user.value
        const password = this.password.value   
        const ls_user = cookie.get('user')
        if(ls_user){
            // obj=JSON.parse(ls_user)
            const f = JSON.parse(ls_user).some(item=>{
                if(item.username === username) return true
            })
            if(f){
                Toast.offline('用户名已存在',2)
                this.user.value='';
                this.password.value='';
            }else{
                 obj.push({username,password})
                cookie.set('user',JSON.stringify( obj ),30)
                Toast.success('注册成功', 2);
                this.props.history.push('/mine/login')
            }
        }else{
             obj.push({username,password})
            cookie.set('user',JSON.stringify( obj ),30)
            this.props.history.push('/mine/login')
            Toast.success('注册成功', 2);
        }


    }

    componentDidMount () {
        this.props.change_title('注册')
    }
    render(){
        return(
            <RegisterContainer>
                <RegisterTitle>
                    <span>-----</span><em>用户注册账号</em><span>-----</span>
                </RegisterTitle>
                <RegisterMidContainer>
                    <input type="text" placeholder="请输入用户名" ref={el=>this.user = el}/>
                    <input type="password" placeholder="6-11位登录密码" 
                    ref={el=>this.password = el}/>
                </RegisterMidContainer>
               
                <RegisterBtn onClick={this.reg}>
                    注册
                </RegisterBtn>
                <RegisterTips>点击注册，表示同意《聚美优品用户协议》</RegisterTips>
            </RegisterContainer>
        )
    }
}
export default HomeCommon(Register)